package com.heima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.reggie.bean.User;
import com.heima.reggie.mapper.UserMapper;
import com.heima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;
    @Autowired
    private RedisTemplate redisTemplate;
/**
  * 根据phone查询
  * @param
  * @param
  * @return
  */
    @Override
    public User selectByPhone(String phone,String code) {
        String Key = "login_"+ phone;
        String code1 = (String) redisTemplate.opsForValue().get(Key);
        log.debug("redis中取出来的手机号{}验证码{}",phone,code1);

        if (code1.equalsIgnoreCase(code)) {
            return mapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone,phone).eq(User::getStatus,1));
        }
        else {
            return new User("0");
        }
        }
/**
  * 新增
  * @param
  * @param
  * @return
  */
    @Override
    public int add(User user) {
        user.setStatus(1);
        return mapper.insert(user);
    }

    @Override
    public User selectByUid(Long currentId) {
        return mapper.selectById(currentId);
    }
}
